#include "AdvertiseCom.h"











int ble_start_advertise(char *bt_name)
{
   if(strlen(name) <= 0)
		return -1;
   /*open bt dev*/
   int device_id = hci_get_route(NULL);
   int device_handle = 0;
   int ret = 0;
   uint8_t status;
   if((device_handle = hci_open_dev(device_id)) < 0)
   {
     printf("Could not open device");
     return -1;
   }

   /*config adv parameters*/
   	struct hci_request rq;
	le_set_advertise_enable_cp advertise_cp;
	le_set_advertising_parameters_cp adv_params_cp;
	le_set_advertising_data_cp adv_data;


    memset(&adv_params_cp, 0, sizeof(adv_params_cp));
	adv_params_cp.min_interval = htobs(0x0800);
	adv_params_cp.max_interval = htobs(0x0800);
	adv_params_cp.chan_map = 7;

	memset(&rq, 0, sizeof(rq));
	rq.ogf = OGF_LE_CTL;
	rq.ocf = OCF_LE_SET_ADVERTISING_PARAMETERS;
	rq.cparam = &adv_params_cp;
	rq.clen = LE_SET_ADVERTISING_PARAMETERS_CP_SIZE;
	rq.rparam = &status;
	rq.rlen = 1;

	ret = hci_send_req(device_handle, &rq, 1000);
	if (ret < 0)
		goto done;

    /*config adv name*/
	memset(&adv_data, 0, sizeof(adv_data));
	adv_data.data[0] = 0x02; 	/* Field length */
	adv_data.data[1] = 0x01; 	/* Flags */
	adv_data.data[2] |= 0x06;	/* BR/EDR Not Supported */

	adv_data.data[3] = 0x03; 	/* Field length */
	adv_data.data[4] = 0xFF; 	/* Vendor field */
	adv_data.data[5] = 0x48; 	/* K */
	adv_data.data[6] = 0x43; 	/* C */

	adv_data.data[7] = 0x03; 	/* Field length */
	adv_data.data[8] = 0x3E; 	/* Version field */
	adv_data.data[9] = 0x04; 	/* current version */
	adv_data.data[10] = 0x01; 	/* minimum version */
	//adv_data.data[7] = 0x02; 	/* Field length */
	//adv_data.data[8] = 0x3E; 	/* Version field */
	//adv_data.data[9] = 0x02; 	/* current version */
	
	adv_data.data[11] = strlen(bt_name) + 1; 	/* Length */
	adv_data.data[12] = 0x08; 	/* local name */
	memcpy(adv_data.data+13,bt_name,strlen(bt_name));
	//adv_data.data[10] = strlen(name) + 1; 	/* Length */
	//adv_data.data[11] = 0x08; 	/* local name */
	//memcpy(adv_data.data+12,name,strlen(name));
	
	adv_data.length = 1 + adv_data.data[0] + 1 + adv_data.data[3] + 1 + adv_data.data[7] + 1 + adv_data.data[11];
	//adv_data.length = 1 + adv_data.data[0] + 1 + adv_data.data[3] + 1 + adv_data.data[7] + 1 + adv_data.data[10];
	memset(&rq, 0, sizeof(rq));
	rq.ogf = OGF_LE_CTL;
	rq.ocf = OCF_LE_SET_ADVERTISING_DATA;
	rq.cparam = &adv_data;
	rq.clen = LE_SET_ADVERTISING_DATA_CP_SIZE;	
	ret = hci_send_req(device_handle, &rq, 1000);
	if (ret < 0)
		goto done;
    
    /*enable adv*/
    memset(&advertise_cp, 0, sizeof(advertise_cp));
	advertise_cp.enable = 0x01;

	memset(&rq, 0, sizeof(rq));
	rq.ogf = OGF_LE_CTL;
	rq.ocf = OCF_LE_SET_ADVERTISE_ENABLE;
	rq.cparam = &advertise_cp;
	rq.clen = LE_SET_ADVERTISE_ENABLE_CP_SIZE;
	rq.rparam = &status;
	rq.rlen = 1;

	ret = hci_send_req(dd, &rq, 1000);

done:
	hci_close_dev(dd);

	if (ret < 0) {
		printf("Can't set advertise mode on hci%d: %s (%d)\n",bt_di.dev_id, strerror(errno), errno);
		return -1;
	}

	if (status) {
		printf("LE set advertise enable on hci%d returned status %d\n",bt_di.dev_id, status);
		return -1;
	}

	return 0;

}




int ble_stop_advertise()
{




}